package com.ruoyi.blindBoxManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.blindBoxManage.domain.BlindBoxPublicProduct;
import com.ruoyi.blindBoxManage.domain.BlindProduct;
import com.ruoyi.blindBoxManage.mapper.*;
import com.ruoyi.blindBoxManage.service.IBlindProductService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 盲盒商品Controller
 *
 * @author ruoyi
 * @date 2025-09-22
 */
@RestController
@RequestMapping("/blindProduct/blindProduct")
public class BlindProductController extends BaseController {
    @Autowired
    private IBlindProductService blindProductService;
    @Resource
    private ClassMapper classMapper;
    @Resource
    private ProductTypeMapper productTypeMapper;
    @Resource
    private BlindBoxMapper blindBoxMapper;
    @Resource
    private BlindBoxPublicProductMapper publicProductMapper;

    /**
     * 查询盲盒商品列表
     */
    @PreAuthorize("@ss.hasPermi('blindProduct:blindProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlindProduct blindProduct) {
        if (!StrUtil.isEmpty(blindProduct.getClassName())) {
            Integer classId = classMapper.selectIdByName(blindProduct.getClassName());
            if (classId != null) {
                blindProduct.setClassId(classId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(blindProduct.getBlindBoxName())) {
            Long blindId = blindBoxMapper.selectIdByName(blindProduct.getBlindBoxName());
            if (blindId != null) {
                blindProduct.setBlindBoxId(blindId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(blindProduct.getProductTypeName())) {
            Integer productTypeId = productTypeMapper.selectIdByName(blindProduct.getProductTypeName());
            if (productTypeId != null) {
                blindProduct.setProductTypeId(productTypeId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<BlindProduct> list = blindProductService.selectBlindProductList(blindProduct);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindProduct boxProduct : list) {
                BlindBoxPublicProduct blindBoxPublicProduct = publicProductMapper.selectBlindBoxPublicProductById(boxProduct.getPublicProductId());
                if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                    boxProduct.setSkuCount(0);
                } else {
                    boxProduct.setSkuCount(blindBoxPublicProduct.getSkuCount());
                }
                String blindBoxName = blindBoxMapper.selectNameById(boxProduct.getBlindBoxId());
                boxProduct.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String productTypeName = productTypeMapper.selectNameById(boxProduct.getProductTypeId());
                boxProduct.setProductTypeName(!StrUtil.isEmpty(productTypeName) ? productTypeName : "");
                String className = classMapper.selectNameById(boxProduct.getClassId());
                boxProduct.setClassName(!StrUtil.isEmpty(className) ? className : "");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出盲盒商品列表
     */
    @PreAuthorize("@ss.hasPermi('blindProduct:blindProduct:export')")
    @Log(title = "盲盒商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlindProduct blindProduct) {
        List<BlindProduct> list = blindProductService.selectBlindProductList(blindProduct);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindProduct boxProduct : list) {
                BlindBoxPublicProduct blindBoxPublicProduct = publicProductMapper.selectBlindBoxPublicProductById(boxProduct.getPublicProductId());
                if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                    boxProduct.setSkuCount(0);
                } else {
                    boxProduct.setSkuCount(blindBoxPublicProduct.getSkuCount());
                }
                String blindBoxName = blindBoxMapper.selectNameById(boxProduct.getBlindBoxId());
                boxProduct.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String productTypeName = productTypeMapper.selectNameById(boxProduct.getProductTypeId());
                boxProduct.setProductTypeName(!StrUtil.isEmpty(productTypeName) ? productTypeName : "");
                String className = classMapper.selectNameById(boxProduct.getClassId());
                boxProduct.setClassName(!StrUtil.isEmpty(className) ? className : "");
            }
        }
        ExcelUtil<BlindProduct> util = new ExcelUtil<BlindProduct>(BlindProduct.class);
        util.exportExcel(response, list, "盲盒商品数据");
    }

    /**
     * 获取盲盒商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('blindProduct:blindProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BlindProduct blindProduct = blindProductService.selectBlindProductById(id);
        if (!ObjectUtils.isEmpty(blindProduct)) {
            BlindBoxPublicProduct blindBoxPublicProduct = publicProductMapper.selectBlindBoxPublicProductById(blindProduct.getPublicProductId());
            if (ObjectUtils.isEmpty(blindBoxPublicProduct)) {
                blindProduct.setSkuCount(0);
            } else {
                blindProduct.setSkuCount(blindBoxPublicProduct.getSkuCount());
            }
            String blindBoxName = blindBoxMapper.selectNameById(blindProduct.getBlindBoxId());
            blindProduct.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
            String productTypeName = productTypeMapper.selectNameById(blindProduct.getProductTypeId());
            blindProduct.setProductTypeName(!StrUtil.isEmpty(productTypeName) ? productTypeName : "");
            String className = classMapper.selectNameById(blindProduct.getClassId());
            blindProduct.setClassName(!StrUtil.isEmpty(className) ? className : "");
        }
        return success(blindProduct);
    }

    /**
     * 新增盲盒商品
     */
    @PreAuthorize("@ss.hasPermi('blindProduct:blindProduct:add')")
    @Log(title = "盲盒商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlindProduct blindProduct) {
        return toAjax(blindProductService.insertBlindProduct(blindProduct));
    }

    /**
     * 修改盲盒商品
     */
    @PreAuthorize("@ss.hasPermi('blindProduct:blindProduct:edit')")
    @Log(title = "盲盒商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlindProduct blindProduct) {
        return toAjax(blindProductService.updateBlindProduct(blindProduct));
    }

    /**
     * 删除盲盒商品
     */
    @PreAuthorize("@ss.hasPermi('blindProduct:blindProduct:remove')")
    @Log(title = "盲盒商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blindProductService.deleteBlindProductByIds(ids));
    }
}
